package com.txsj.scan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.AllocationApplyBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 调拨申请单列表适配器
 */
public class AllocationApplyListAdapter extends BaseExpandableListAdapter {


    private Context context;
    private List<AllocationApplyBean> list = new ArrayList<>();

    public AllocationApplyListAdapter(Context context) {
        this.context = context;
    }

    public List<AllocationApplyBean> getList() {
        return list;
    }

    public void setList(List<AllocationApplyBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return list.size() == 0 ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        AHGroupHolder groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_allocation_apply_group, null, false);
            groupHolder = new AHGroupHolder(convertView);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (AHGroupHolder) convertView.getTag();
        }
        SimpleDateFormat simpleDateFormat0 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat0.parse(list.get(groupPosition).getAdd_time());
            groupHolder.tvDate.setText(simpleDateFormat1.format(date));
            groupHolder.tvCount.setText(list.get(groupPosition).getList().size() + "单");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        AHChildHolder childHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_allocation_apply_item, null);
            childHolder = new AHChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (AHChildHolder) convertView.getTag();
        }
        childHolder.tv_form_num.setText(list.get(groupPosition).getList().get(childPosition).getBill_number());
        childHolder.tv_form_state.setText(list.get(groupPosition).getList().get(childPosition).getBill_status_name());
        childHolder.tv_form_Warehouse.setText(list.get(groupPosition).getList().get(childPosition).getInto_warehouse_name());
        String fromInfo = "共 " + list.get(groupPosition).getList().get(childPosition).getGoods_category_count() + " 种商品，共计 "
                + list.get(groupPosition).getList().get(childPosition).getGoods_count() + " 件";
        childHolder.tv_form_goods.setText(fromInfo);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (allocateHistoryAdapeterinterface != null) {
                    allocateHistoryAdapeterinterface.setOnChildClickListener(groupPosition, childPosition);
                }
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class AHGroupHolder {

        TextView tvDate, tvCount;

        public AHGroupHolder(View view) {
            tvDate = (TextView) view.findViewById(R.id.tv_group_date);
            tvCount = (TextView) view.findViewById(R.id.tv_group_Itemnum);
        }
    }

    class AHChildHolder {

        TextView tv_form_num;
        TextView tv_form_state;
        TextView tv_form_Warehouse;
        TextView tv_form_goods;

        public AHChildHolder(View itemView) {
            tv_form_num = (TextView) itemView.findViewById(R.id.tv_form_num);
            tv_form_state = (TextView) itemView.findViewById(R.id.tv_form_state);
            tv_form_Warehouse = (TextView) itemView.findViewById(R.id.tv_form_Warehouse);
            tv_form_goods = (TextView) itemView.findViewById(R.id.tv_form_goods);
        }
    }

    public void setAllocateHistoryAdapeterinterface(AllocateHistoryAdapeterinterface allocateHistoryAdapeterinterface) {
        this.allocateHistoryAdapeterinterface = allocateHistoryAdapeterinterface;
    }

    public AllocateHistoryAdapeterinterface allocateHistoryAdapeterinterface;

    public interface AllocateHistoryAdapeterinterface {
        void setOnChildClickListener(int groupPosition, int childPosition);
    }
}
